import 'package:flutter/material.dart';
import 'package:flutter_utils/flutter_utils.dart';
import 'package:flutter_widget/src/config/color_define.dart';
import 'package:flutter_widget/src/config/font.dart';

///通用文本Widget
class TextFormatWidget extends StatelessWidget {
  final String? text;
  final double fontSize;
  final Color? color;
  final double? letterSpacing; //间距
  final FontWeight? fontWeight; //建议使用 FontWeightTool！
  final double? height; //行高, （对齐tip:全英文数字最少1.0，有中文最少1.2）ps:SourceHanBold行高1.1！！！
  final double? leading;
  final int maxLine; //最多行数（> 0 时，超出字符变成...省略号; = 0时 不限行）
  final String? fontName; //字体名称（eg:FontName.SourceHanBold）
  final TextStyle? textStyle; //可自定义样式
  final TextAlign? textAlign;
  final bool isShadow;
  final double? shadowElevation; //阴影扩散程度
  final Color? shadowColor; //阴影颜色
  final Offset? shadowOffset;

  const TextFormatWidget(
    this.text, {
    Key? key,
    this.fontSize = 14,
    this.color,
    this.letterSpacing,
    this.fontWeight,
    this.height,
    this.leading = 0,
    this.maxLine = 0,
    this.fontName,
    this.textStyle,
    this.isShadow = false,
    this.textAlign,
    this.shadowElevation,
    this.shadowColor,
    this.shadowOffset,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    TextStyle style;
    List<Shadow> shadows = isShadow == true
        ? [
            Shadow(
              color: shadowColor ?? Colors.black.withOpacity(0.15),
              offset: shadowOffset ?? const Offset(0, 1),
              blurRadius: shadowElevation ?? 1,
            ),
          ]
        : [];
    if (fontName == null) {
      style = textStyle ??
          TextStyle(
            fontSize: fontSize,
            fontWeight: fontWeight ?? FWFontTool.w400,
            color: color ?? FWColorPicker.white,
            letterSpacing: letterSpacing ?? 0,
            height: height,
            decoration: TextDecoration.none,
            shadows: shadows,
            textBaseline: TextBaseline.ideographic,
          );
    } else {
      style = textStyle ??
          TextStyle(
            fontFamily: fontName,
            fontSize: fontSize,
            fontWeight: fontWeight ?? FWFontTool.w600,
            color: color ?? FWColorPicker.white,
            letterSpacing: letterSpacing ?? 0,
            height: height,
            decoration: TextDecoration.none,
            shadows: shadows,
            textBaseline: TextBaseline.ideographic,
          );
    }
    StrutStyle strutStyle = StrutStyle(
      fontSize: style.fontSize,
      fontWeight: style.fontWeight,
      leading: leading,
      height: height,
      fontStyle: style.fontStyle,
      forceStrutHeight: true, // 关键属性 强制改为文字高度
    );

    //解决iOS部分数字emoji显示问题
    String _text = TextUtil.displayEmojiText(text ?? "");
    // String _text = text ?? "";
    if (maxLine > 0) {
      return Text(
        TextUtil.breakWord(_text),
        textAlign: textAlign ?? TextAlign.left,
        style: style,
        maxLines: maxLine,
        overflow: TextOverflow.ellipsis,
        strutStyle: strutStyle,
        textScaleFactor: 1.0,
      );
    } else {
      return Text(
        _text,
        textAlign: textAlign ?? TextAlign.left,
        style: style,
        // overflow: TextOverflow.ellipsis,
        strutStyle: strutStyle,
        textScaleFactor: 1.0,
      );
    }
  }
}
